// When color definition differs for dark and light variant,
// it gets @if ed depending on $variant

@use "sass:color";
@use "sass:math";
@use "sass:string";
@use "theme";

@function gtkalpha($color, $alpha) {
  @return string.unquote("alpha(#{$color}, #{$alpha})");
}

// Determine whether the color has alpha.
@function _has-alpha($color) {
  @return if(color.alpha($color) < 1, true, false);
}

// Determine whether the color is dark.
@function _is-dark($color) {
  // Calculate color brightness as per https://www.w3.org/TR/AERT/#color-contrast
  $brightness: (color.red($color) * 299 + color.green($color) * 587 + color.blue($color) * 114) / 1000;

  @return if($brightness < 128, true, false);
}

$on-light: #000;
$on-dark: #fff;

// Determine whether to use dark or light color on top of given color
// to meet accessibility standards for contrast.
@function on($color) {
  $contrast-color: if(_is-dark($color), $on-dark, $on-light);

  @if color.saturation($color) > 50% or color.alpha($color) < 1 {
    @return $contrast-color;
  } @else {
    @return rgba($contrast-color, .87);
  }
}

// Determine the strength of highlight on top of given color.
@function highlight($color) {
  @if color.lightness($color) >= 80% {
    @return rgba(white, .4);
  } @else if color.lightness($color) >= 40% {
    @return rgba(white, .2);
  } @else {
    @return rgba(white, .05);
  }
}

// Make translucent color opaque by blending with the background color.
@function opacify($color, $on) {
  @return color.mix(color.change($color, $alpha: 1), $on, color.alpha($color) * 100%);
}

//
// Main colors
//

$surface-z0: #f9f9f9;
$surface-z1: #fff;
$surface-z8: #fff;
$surface-switch-thumb: #fff;
$on-surface: on($surface-z0);

$primary-on-light: #1967d2;
$primary-on-dark: #8ab4f8;
$primary: $primary-on-light;
$on-primary: on($primary);

$error-on-light: #d93025;
$error-on-dark: #f28b82;
$error: $error-on-light;
$on-error: on($error);

$warning-on-light: #f9ab00;
$warning-on-dark: #fdd663;
$warning: $warning-on-light;
$on-warning: on($warning);

$success-on-light: #1e8e3e;
$success-on-dark: #81c995;
$success: $success-on-light;
$on-success: on($success);

$visited-on-light: #9334e6;
$visited-on-dark: #c58af9;
$visited: $visited-on-light;
$on-visited: on($visited);

$system: #212121;
$on-system: on($system);

$tooltip: rgba(#616161, .9);
$on-tooltip: on($tooltip);

$scrim: rgba(black, .6);
$on-scrim: on($scrim);

$scrim-alt: rgba(black, .3);
$on-scrim-alt: on($scrim-alt);

$panel: #212121;
$on-panel: on($panel);

// for Unity panel which doesn't allow translucent colors
$panel-solid: $panel;
$on-panel-solid: on($panel-solid);

$titlebar: #424242;
$titlebar-backdrop: #303030;
$on-titlebar: on($titlebar);

$titlebar-indicator: currentcolor;

@if theme.$dark-theme {
  $surface-z0: #121212;
  $surface-z1: color.mix(white, $surface-z0, 5%);
  $surface-z8: color.mix(white, $surface-z0, 12%);
  $surface-switch-thumb: color.mix(white, $surface-z0, 60%);
  $on-surface: on($surface-z0);

  $primary: $primary-on-dark;
  $on-primary: on($primary);

  $error: $error-on-dark;
  $on-error: on($error);

  $warning: $warning-on-dark;
  $on-warning: on($warning);

  $success: $success-on-dark;
  $on-success: on($success);

  $visited: $visited-on-dark;
  $on-visited: on($visited);

  $titlebar: color.mix(white, $surface-z0, 9%);
  $titlebar-backdrop: $surface-z1;
  $on-titlebar: on($titlebar);
}

@if theme.$light-topbar {
  $panel: $scrim;
  $on-panel: on($panel);

  $panel-solid: #e0e0e0;
  $on-panel-solid: on($panel-solid);

  $titlebar: #f0f0f0;
  $titlebar-backdrop: #ebebeb;
  $on-titlebar: on($titlebar);

  $titlebar-indicator: $primary;
}

//
// Overlay state colors
//

// Determine the overlay color depending on the given color and opacity.
@function _overlay($color, $opacity, $on) {
  $opacity-modifier: 0;

  @if color.saturation($color) > 50% or color.saturation($on) > 50% {
    $opacity-modifier: .04;
  }

  @return color.mix(rgba($color, 1), $on, math.percentage($opacity + $opacity-modifier));
}

@function hover-overlay($color, $opacity-modifier: 0, $on: transparent, $alt: false) {
  $opacity: .08;

  @if $alt {
    $opacity: .04;
  }

  @return _overlay($color: $color, $opacity: $opacity + $opacity-modifier, $on: $on);
}

@function focus-overlay($color, $opacity-modifier: 0, $on: transparent) {
  $opacity: .08;

  @return _overlay($color: $color, $opacity: $opacity + $opacity-modifier, $on: $on);
}

@function pressed-overlay($color, $opacity-modifier: 0, $on: transparent) {
  $opacity: .12;

  @return _overlay($color: $color, $opacity: $opacity + $opacity-modifier, $on: $on);
}

@function dragged-overlay($color, $opacity-modifier: 0, $on: transparent) {
  $opacity: .08;

  @return _overlay($color: $color, $opacity: $opacity + $opacity-modifier, $on: $on);
}

@function activated-overlay($color, $opacity-modifier: 0, $on: transparent) {
  $opacity: .12;

  @return _overlay($color: $color, $opacity: $opacity + $opacity-modifier, $on: $on);
}

@function selected-overlay($color, $opacity-modifier: 0) {
  $opacity: .24;

  @if $color == $on-surface {
    $color: $primary;
  }

  @return rgba($color, $opacity + $opacity-modifier);
}

$selected-overlay: selected-overlay($on-surface);

//
// For “on” colors
//

@function primary($color) {
  @return if(_is-dark($color), $primary-on-light, $primary-on-dark);
}

@function error($color) {
  @return if(_is-dark($color), $error-on-light, $error-on-dark);
}

@function hint($color) {
  @return if(_has-alpha($color), rgba($color, .6), rgba($color, .7));
}

@function disabled($color) {
  @return if(_has-alpha($color), rgba($color, .38), rgba($color, .5));
}

@function disabled-hint($color) {
  @return if(_has-alpha($color), rgba($color, .26), rgba($color, .3));
}

@function stroke($color) {
  @return if(_has-alpha($color), rgba($color, .26), rgba($color, .3));
}

@function disabled-stroke($color) {
  @return if(_has-alpha($color), rgba($color, .12), rgba($color, .2));
}

@function divider($color) {
  @return if(_has-alpha($color), rgba($color, .12), rgba($color, .2));
}

@function fill($color) {
  @return if(_has-alpha($color), rgba($color, .08), rgba($color, .08));
}

@function entry-fill($color) {
  @return if(_has-alpha($color), rgba($color, .04), rgba($color, .04));
}

@function scrollbar-thumb($color, $state: null) {
  @if $state == null {
    @return if(_has-alpha($color), rgba($color, .38), rgba($color, .5));
  } @else if $state == "hover" {
    @return if(_has-alpha($color), rgba($color, .48), rgba($color, .6));
  } @else if $state == "pressed" {
    @return if(_has-alpha($color), rgba($color, .6), rgba($color, .7));
  } @else if $state == "disabled" {
    @return if(_has-alpha($color), rgba($color, .26), rgba($color, .3));
  } @else {
    @error "Invalid type: '#{$state}'";
  }
}
